/*
 * @Author: 小花
 * @Date: 2022-04-09 19:30:10
 * @LastEditors: 小花
 * @LastEditTime: 2022-04-14 09:15:37
 * @FilePath: /vue3-teap/src/plugins/rsa.ts
 * @Description:RSA
 */
import JSEncrypt from 'jsencrypt'
import { Md5 } from 'ts-md5/dist/md5'

/**
 * @description: RSA加密
 * @param {string} publicKey 公钥
 * @param {string} str 要加密的字符串
 * @return {*} base64编码的加密字符串
 */
export const pubKeyEncrypt = (publicKey: string, str: string): string | false => {
  const encryptor = new JSEncrypt()
  encryptor.setPublicKey(publicKey)
  const result = encryptor.encrypt(str)
  return result
}

/**
 * @description: RSA解密
 * @param {string} privateKey 私钥
 * @param {string} str 要解密的Base64加密字符串
 * @return {*} 解密的字符串
 */
export const prikeyDecrypt = (privateKey: string, str: string) => {
  const decryptor = new JSEncrypt()
  decryptor.setPrivateKey(privateKey)
  const resurl = decryptor.decrypt(str)
  return resurl
}

/**
 * @description: 签名
 * @param {string} publicKey 公钥
 * @param {string} str 要验证的字符串
 * @param {string} signature base64编码的签名
 * @return {*} 是否匹配数据和签名
 */
export const verify = (publicKey: string, str: string, signature: string): boolean => {
  const verify = new JSEncrypt()
  verify.setPublicKey(publicKey)
  return verify.verify(str, signature, publicKey => Md5.hashAsciiStr(publicKey))
}

export const publicKey = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDeezw5tkJxPU03cgZH9iZVAU3H
IQ65nTEaxchlIYs89rxITOB62ip+w4LokHSJ65K6S1nqfMgs1QRphLgTJhx0EtWz
tfK8ACnVb3nByOLl2pUxu0DjKbbeJGeZjzlndODyjGnaPPgpx+0JIOo53dWwVJYD
ywUKd2uAUR/ehkObCwIDAQAB
-----END PUBLIC KEY-----
`
export const privateKey = `-----BEGIN PRIVATE KEY-----
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAN57PDm2QnE9TTdy
Bkf2JlUBTcchDrmdMRrFyGUhizz2vEhM4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